#include "test2.hpp"
#include "VmModel.hpp"
#include "block.hpp"
#include "config.hpp"
#include "rander.hpp"
#include <cstdio>

inline static void print_block(mem_block const &mb) {
  if (mb.is_valid) {
    printf("%d ", mb.num);
  } else {
    printf("_ ");
  }
}

static void print_helper(std::vector<int> const &requests,
                         std::vector<std::vector<mem_block>> const &res,
                         int page_fault, ALGO algo) {
  auto algo_str = algo == OPT ? "OPT" : (algo == LRU ? "LRU" : "FIFO");
  printf("算法：%s\n缺页数：%d 缺页率：%f\n", algo_str, page_fault,
         float(page_fault) / res.size());
  printf("页面号序列为：\n");
  for (auto const &item : requests) {
    printf("%d ", item);
  }
  printf("\n结果数列为：\n");
  for (int i = 0; i < N_MEM; ++i) {
    for (auto const &item : res) {
      print_block(item[i]);
    }
    printf("\n\n");
  }
}

static void test2_helper(ALGO algo, int request_size) {
  rander rander(0, VM_N_BLOCK - 1);
  std::vector<int> requests(rander.get_rand_vector(request_size));

  VmModel vmModel(PAGE_SIZE, VM_N_BLOCK, N_MEM, algo);

  std::vector<std::vector<mem_block>> res;
  auto page_fault = vmModel.simulate(requests, res);
  print_helper(requests, res, page_fault, algo);
}

void test2(int request_size) {
  test2_helper(OPT, request_size);
  test2_helper(LRU, request_size);
  test2_helper(FIFO, request_size);
}